<?php

class LogManager extends BaseModule {

	public function __construct() {
		parent::__construct();

		$fileValue = file_get_contents(SYSTEMCONFIGPATH);
		$fileValueList = explode("\n", $fileValue);
		foreach ($fileValueList as $value) {
			if (strpos($value, "=") !== false) {
				$configOne = explode("\t", $value);
				$configOneList = explode("=", $configOne[0]);
				$configOneEName = substr($configOneList[0], 0, -1);
				if ($configOneEName === "log_count") {
					$configOneValue = substr($configOneList[1], 1);
					define('LOGMAXCOUNT', $configOneValue);
					break;
				}
			}
		}
	}

	/**
	 * 显示页面方法
	 * @param string $type  list为列表页面，export为导出
	 * @return array
	 */
	public function pageShow($type = "list", $controllerName) {
		$pageKey = $_GET ["pageKey"];
		require_rewrite(DATA_PATH . "LogPageConfig.class.php");

		if (is_string($pageKey) && array_key_exists($pageKey, LogPageConfig::$pageConfig)) {
			$pageConfig = LogPageConfig::$pageConfig [$pageKey];
		} else {
			$this->errorAndExit("页面配置未找到！");
		}

		$module = new LogModule ();
		$returnList = $module->pageShow($pageConfig, $type, $controllerName);
		$returnList ['pageKey'] = $pageKey;
		return $returnList;
	}

	public function moreShow($controllerName) {
		$pageKey = $_GET ["pageKey"];
		$logId = $_GET ["logId"];
		require_rewrite(DATA_PATH . "LogPageConfig.class.php");

		if (is_string($pageKey) && array_key_exists($pageKey, LogPageConfig::$pageConfig) && LogPageConfig::$pageConfig [$pageKey]["showMore"]) {
			$pageConfig = LogPageConfig::$pageConfig [$pageKey];
		} else {
			$this->errorAndExit("页面配置未找到！");
		}

		$module = new LogModule ();
		$returnList = $module->moreShow($pageConfig, $logId, $controllerName);
		$returnList ['pageKey'] = $pageKey;
		return $returnList;
	}

	public function moreImg() {
		$table = $_GET ["table"];
		$field = $_GET ["field"];
		$logId = $_GET ["logId"];

		require_rewrite(DATA_PATH . "LogPageConfig.class.php");
		if (is_string($table) && array_key_exists($table, LogPageConfig::$pageConfig)) {
			$pageConfig = LogPageConfig::$pageConfig [$table];
		} else {
			exit();
		}

		$where = " {$pageConfig['logId']} = {$logId} ";

		$module = new LogModule ();
		$returnList = LogDao::getImg($table, $field, $where);

		Header("Content-type: image/jpg");
		echo $returnList[0][$field];
	}

	/**
	 * 写管理员登陆日志
	 *
	 * @param string $userId      管理员ID  	
	 * @param string $userName    管理员名称
	 * @param int $loginStatus    登陆状态0失败，1成功
	 * @param int $isAudit		   是否在audit管理员日志里展示，1为展示0为不展示
	 * @return boolean		写入状态
	 */
	public static function writeAdminLoginLog($userId, $userName, $loginStatus, $isAudit = 0) {
		$data = array();

		$data ['loginTime'] = "now()";
		$data ['loginIp'] = "'" . Handler_http::getIP() . "'";
		$data ['userId'] = "'" . $userId . "'";
		$data ['userName'] = "'" . $userName . "'";
		$data ['loginStatus'] = "'" . $loginStatus . "'";
		$data ['isAudit'] = "'" . $isAudit . "'";
		$data ['isExport'] = "0";

		$module = new LogModule ();
		return $module->writeLog('adminlogin', $data);
	}

	/**
	 * 写账号维护日志
	 *
	 * @param string $userId   		用户ID
	 * @param string $userName      用户名称
	 * @param string $description   账号修改描述
	 * @param int $isAudit		   是否在audit管理员日志里展示，1为展示0为不展示
	 * @return boolean		写入状态
	 */
	public static function writeAdminModifyLog($userId, $userName, $description, $isAudit = 0) {
		$data = array();

		$data ['date'] = "now()";
		$data ['ip'] = "'" . Handler_http::getIP() . "'";
		$data ['userId'] = "'" . $userId . "'";
		$data ['userName'] = "'" . $userName . "'";
		$data ['description'] = "'" . $description . "'";
		$data ['isAudit'] = "'" . $isAudit . "'";
		$data ['isExport'] = "0";

		$module = new LogModule ();
		return $module->writeLog('adminmodify', $data);
	}
	
	/**
	 * 写账号操作日志
	 *
	 * @param string $userId   		用户ID
	 * @param string $userName      用户名称
	 * @param string $description   账号修改描述
	 * @param int $isAudit		   是否在audit管理员日志里展示，1为展示0为不展示
	 * @return boolean		写入状态
	 */
	public static function writeAdminOperationLog($userId, $userName, $description, $isAudit = 0) {
		$data = array();

		$data ['date'] = "now()";
		$data ['ip'] = "'" . Handler_http::getIP() . "'";
		$data ['userId'] = "'" . $userId . "'";
		$data ['userName'] = "'" . $userName . "'";
		$data ['description'] = "'" . $description . "'";
		$data ['isAudit'] = "'" . $isAudit . "'";
		$data ['isExport'] = "0";

		$module = new LogModule ();
		return $module->writeLog('operation', $data);
	}

	/**
	 * 写审计操作日志
	 * @param string $userId		提交用户ID
	 * @param string $userName		提交用户用户名
	 * @param int $logId			对应日志ID
	 * @param int $type				操作类型，1用户，2文件，3系统配置文件，4网站域名
	 * @param int $action			操作动作，1添加/上传，2修改
	 * @return boolean		写入状态
	 */
	public static function writeAuditLog($userId, $userName, $logId, $type, $action) {
		$data = array();

		$data ['createDateTime'] = "now()";
		$data ['operator'] = "'" . $operator . "'";
		$data ['operatorName'] = "'" . $operatorName . "'";
		$data ['userId'] = "'" . $userId . "'";
		$data ['userName'] = "'" . $userName . "'";
		$data ['logId'] = "'" . $logId . "'";
		$data ['type'] = "'" . $type . "'";
		$data ['action'] = "'" . $action . "'";
		$data ['result'] = "0";
		$data ['isExport'] = "0";

		$module = new LogModule ();
		return $module->writeLog('audit', $data);
	}

	/**
	 * 更新审计日志：审计操作记录接口
	 * @param int $auditId			审计日志的ID
	 * @param int $operator			审批者ID
	 * @param string $operatorName	审批者名称
	 * @param int $result			审批结果：1成功，2失败
	 * @return boolean		更新状态
	 */
	public static function updateAuditLog($auditId, $operator, $operatorName, $result) {
		//var_dump($auditId);exit;
		$data = array();

		$data ['audiDateTime'] = "now()";
		$data ['operator'] = "'" . $operator . "'";
		$data ['operatorName'] = "'" . $operatorName . "'";
		$data ['auditIp'] = "'" . Handler_http::getIP() . "'";
		$data ['result'] = "'" . $result . "'";

		$module = new LogModule ();
		return $module->updateLog('audit', $data, array(
					id => $auditId
		));
	}

	/**
	 * 写入日志导出记录
	 * @param int $adminId			管理员ID
	 * @param string $adminName		管理员名称
	 * @param int $downType			下载类型：0 excel，1 txt
	 * @param string $logType		日志类型：对应表名，adminlogin、adminmodify、audit、downlog、uploadfile
	 * @param string $downloadFile	下载文件名称
	 * @return boolean		写入状态
	 */
	public static function writeDownLog($adminId, $adminName, $downType, $logType, $downloadFile) {
		$data = array();

		$data ['dateTime'] = "now()";
		$data ['ip'] = "'" . Handler_http::getIP() . "'";
		$data ['adminId'] = "'" . $adminId . "'";
		$data ['adminName'] = "'" . $adminName . "'";
		$data ['downType'] = "'" . $downType . "'";
		$data ['logType'] = "'" . $logType . "'";
		$data ['downloadFile'] = "'" . $downloadFile . "'";
		$data ['isExport'] = "0";

		$module = new LogModule ();
		return $module->writeLog('downlog', $data);
	}

	/**
	 * 写入文件上传日志
	 * @param int $uploadUser			上传者ID
	 * @param string $uploadUserName	上传者名称
	 * @param string $fileName			文件名
	 * @param int $byte					文件大小
	 * @param int $status				上传状态：0失败，1成功
	 * @return boolean		写入状态
	 */
	public static function writeUploadFileLog($uploadUser, $uploadUserName, $fileName, $byte, $status) {
		$data = array();

		$data ['dateTime'] = "now()";
		$data ['ip'] = "'" . Handler_http::getIP() . "'";
		$data ['uploadUser'] = "'" . $uploadUser . "'";
		$data ['uploadUserName'] = "'" . $uploadUserName . "'";
		$data ['fileName'] = "'" . $fileName . "'";
		$data ['byte'] = "'" . $byte . "'";
		$data ['status'] = "'" . $status . "'";
		$data ['isExport'] = "0";

		$module = new LogModule ();
		return $module->writeLog('uploadfile', $data);
	}

	/**
	 * 错误并退出
	 *
	 * @param string $msg  错误抛出信息     	
	 */
	private function errorAndExit($msg) {
		exit($msg);
	}

}
